﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace eDoc
{
    public partial class Master : System.Web.UI.MasterPage
    {
        private BO.Sescurity _security = new BO.Sescurity();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Request.IsLocal && !Request.IsSecureConnection)
            //{
            //    string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
            //    Response.Redirect(redirectUrl, false);
            //    HttpContext.Current.ApplicationInstance.CompleteRequest();
            //}

            string urlCurent = HttpContext.Current.Request.Url.AbsolutePath.ToLower().Trim();

            //if (Session["Portal_Username"] == null || Session["Portal_Username"].ToString() == "")
            //    Response.Redirect("/login.aspx");
            if (!_security.CheckLogin(HttpContext.Current))
            {
                Response.Redirect("/login.aspx");
            }



            if (_security.CurrentUser(HttpContext.Current).Username != BO.Sescurity.UserRoleAdmin && urlCurent!="error.aspx")
            {

                DataTable dt = new DataTable();

                dt = BO.Data.Select_Menu_by_Url(urlCurent);

                if (dt != null && dt.Rows.Count > 0)
                {
                    string menuID = dt.Rows[0]["ID"].ToString();
                    string[] strMenuID = new string[] { };
                    DataTable dtMenu = BO.Data.Select_User_Menu(_security.CurrentUser(HttpContext.Current).Username);
                    if (dtMenu != null && dtMenu.Rows.Count > 0)
                    {
                        strMenuID = dtMenu.Rows[0]["Menu_ID"].ToString().Split(",".ToCharArray());
                    }
                    if (!BO.Common.CheckMenuID(strMenuID, menuID))
                    {
                        Response.Redirect("/error.aspx");
                    }
                }
            }

        }

        protected void imgLogout_Click(object sender, ImageClickEventArgs e)
        {
            _security.SignOutLocal(HttpContext.Current);
            Session.Abandon();
            Response.Redirect("/login.aspx");
        }

        protected void btLogout_Click(object sender, EventArgs e)
        {
            _security.SignOutLocal(HttpContext.Current);
            Session.Abandon();
            Response.Redirect("/login.aspx");
        }
    }
}